
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../../../pojo/ImPojo.dart';
import '../../../../util/DialogUtil.dart';
import '../../../../util/ImCacheFactory.dart';
import '../../../../util/ImConstant.dart';
import '../../../../util/ImSingleton.dart';
import '../../../bloc/SelectGroupUsersCubit.dart';
import '../../../bloc/WebRtcCubit.dart';
import '../../../sub/SelectGroupUsersPage.dart';
import '../../ListSeparator.dart';
import '../../WebRtcAudioView.dart';
import '../../WebRtcVideoView.dart';

void showVideoAndAudioBottomSheet(BuildContext context, ImSession session) {
  showModalBottomSheet(
    context: context,
    builder: (context) {
      return Container(
        height: 160,
        decoration: const BoxDecoration(
            color: ImConstant.IM_RESERVE_BACKGROUND_COLOR,
            borderRadius: BorderRadius.only(topLeft: Radius.circular(10), topRight: Radius.circular(10))),
        alignment: Alignment.centerLeft,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Material(color: Colors.white, child: InkWell(
              onTap: () => _videoEvent(context, session),
              child: const Padding(padding: EdgeInsets.only(top: 10, bottom: 10), child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                   Icon(Icons.video_call),
                   Padding(
                    padding: EdgeInsets.only(left: 20),
                    child: Text('视频聊天'),
                  ),
                ],

              ),),
            ), ),

            ListSeparator(
              indent: 10,
            ),

            Material(color: Colors.white, child: InkWell(
              onTap: () => _audioEvent(context, session),
              child: const Padding(padding: EdgeInsets.only(top: 10, bottom: 10), child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget> [
                  Icon(Icons.call),
                  Padding(
                    padding: EdgeInsets.only(left: 20),
                    child: Text('语音聊天'),
                  ),
                ],
              ),),
            ), ),

            ListSeparator(
              indent: 10,
            ),

            Container(height: 5, color: ImConstant.IM_MAIN_BACKGROUND_COLOR),

            Material(color: Colors.white, child: InkWell(
              onTap: () {
                Navigator.of(context).pop();
              },
              child: const Padding(
                padding: EdgeInsets.only(top: 10, bottom: 10),
                child: Center(child: Text('取消'),),
              ),),
            ),
          ],

        ),
      );
    },
  );
}

void _videoEvent(BuildContext context, ImSession session) async {
  if (session?.groupCode != null) {
    Navigator.of(context).push(MaterialPageRoute(builder: (context) {
      return BlocProvider(create: (BuildContext context) {
        var existedUserIdSet = <String>{};
        existedUserIdSet.add(session!.fromUserId!);
        return SelectGroupUsersCubit(session!.groupCode!, false, existedUserIdSet)
          ..loadData(existedUserIdSet);
      }, child: SelectGroupUsersPage(groupCode: session!.groupCode!));
    }));
    return;
  }

  var userSet = Set<ImUser>();
  if (session!.toUser == null) {
    var toUser = await ImCacheFactory.getInstance().getImUser(session!.toUserId!);
    userSet.add(toUser!);
  }
  var fromImUser = await ImCacheFactory.getInstance().getImUser(session!.fromUserId!);
  userSet.add(fromImUser!);

  if (context.mounted) {
    var webRtcCubit = context.read<WebRtcCubit>();
    var dialog = WebRtcVideoView();
    OverlayEntry entry = showOverlay(context, dialog);
    dialog.overlayEntry = entry;
    ImSingleton.getInstance().session.putData("webRtcOverlay", entry);

    Navigator.of(context).pop();

    webRtcCubit.add(WebRtcInitiateEvent(context, userSet, entry, false));
    // Navigator.of(context).push(MaterialPageRoute(
    //     builder: (BuildContext context) {
    //       var webRtcCubit = context.read<WebRtcCubit>();
    //       webRtcCubit.add(WebRtcVideoInitEvent(context, null, userSet));
    //       return const WebRtcVideoPage();
    //     })
    // );
  }
}

void _audioEvent(BuildContext context, ImSession session) async {
  if (session?.groupCode != null) { // 选择多人进视频聊天
    Navigator.of(context).push(MaterialPageRoute(builder: (context) {
      return BlocProvider(create: (BuildContext context) {
        var existedUserIdSet = <String>{};
        existedUserIdSet.add(session!.fromUserId!);
        return SelectGroupUsersCubit(session!.groupCode!, true, existedUserIdSet)
          ..loadData(existedUserIdSet);
      }, child: SelectGroupUsersPage(groupCode: session!.groupCode!));
    }));
    return;
  }

  var userSet = Set<ImUser>();
  if (session!.toUser == null) {
    var toUser = await ImCacheFactory.getInstance().getImUser(session!.toUserId!);
    userSet.add(toUser!);
  }
  var fromImUser = await ImCacheFactory.getInstance().getImUser(session!.fromUserId!);
  userSet.add(fromImUser!);
  if (context.mounted) {
    var webRtcCubit = context.read<WebRtcCubit>();
    var dialog = WebRtcAudioView();
    OverlayEntry entry = showOverlay(context, dialog);
    dialog.overlayEntry = entry;
    ImSingleton.getInstance().session.putData("webRtcOverlay", entry);

    Navigator.of(context).pop();

    webRtcCubit.add(WebRtcInitiateEvent(context, userSet, entry, true));
    // Navigator.of(context).push(MaterialPageRoute(
    //     builder: (BuildContext context) {
    //       var webRtcCubit = context.read<WebRtcCubit>();
    //       webRtcCubit.add(WebRtcAudioInitEvent(context, null, userSet));
    //       return const WebRtcAudioPage();
    //     })
    // );
  }
}